package org.itboys.mobile.controller.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.question.SundayQuestionDto;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXinQuestionService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author：贾琦磊
 * @Date：2018/7/19
 * @Description：问答模块接口
 */
@Controller
@RequestMapping("/sunday/small/question")
@Api(value = "question",description = "问答/我的问答")
public class SundaySmallQuestionController extends BaseController{
    @Autowired
    private SundayWeiXinQuestionService questionService;

    @ApiOperation(value = "提问")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public void save(HttpServletRequest request, HttpServletResponse response
    , @ApiParam(value = "用户id",required = true)@RequestParam("memberId")Long memberId
    ,@ApiParam(value = "问题",required = true)@RequestParam("content")String content){
        try {
            questionService.save(memberId,content);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/question/save----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

    @ApiOperation(value = "问答详情")
    @RequestMapping(value = "/getDetail",method = RequestMethod.POST)
    public void getDetail(HttpServletRequest request,HttpServletResponse response
    ,@ApiParam(value = "问答id",required = true)@RequestParam("id")Long id){
        try {
            ResponseMessage.success(questionService.getDetail(id));
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/question/getDetail----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }
    @ApiOperation(value = "【获取问答列表/我的问答】传入用户id将查询我的问答，不传查询所有")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    public void getAllQuestions(HttpServletRequest request,HttpServletResponse response,
                                @ApiParam(value = "用户id")@RequestParam(value = "memberId",required = false) Long memberId
            ,@ApiParam(value = "起始页",required = true) @RequestParam(value = "pageNumber",required = true) String pageNumber
            , @ApiParam(value = "每页大小",required = true) @RequestParam(value = "pageSize",required = true)String pageSize){
        try {
            List<SundayQuestionDto> allQuestions = questionService.getAllQuestions(request,memberId);
            ResponseMessage.success(allQuestions);
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/question/select----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }
}
